package com.trans.auxiliary;


import com.trans.anno.Info;
import com.trans.anno.Translator;
import com.trans.utils.ParamsUtils;
import lombok.Getter;
import lombok.Setter;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author xwduo
 * @Description //TODO
 * @Date 2022-10-20 14:36
 */
public class HandleRecord {

    @Info("注解对象")
    public final Translator translator;

    @Info("字段属性")
    private final Params params;

    @Info("返回的翻译数据对象")
    private final Map<String,Object> item;

    @Info("当前层级的源对象")
    private final Object record;

    @Info("字段属性")
    public final String fieldName;

    @Info("字段属性")
    public final Class type;

    @Info("此次翻译的对象-顶级")
    private final Object fullRecord;

    @Info("重命名-名字")
    @Setter@Getter
    private String rename;

    public HandleRecord(Translator translator, Map<String, Object> item, Object record, Params params, Object fullRecord) {
        this.translator = translator;
        this.item = item;
        this.record = record;
        this.fieldName = params.name;
        this.type = params.type;
        this.fullRecord = fullRecord;
        this.params = params;
    }

    //获取值-当前翻译字段
    public Object getVal(String name){
        return item.get(name);
    }

    //获取值-当前翻译字段
    public Object getVal(){
        return item.get(params.name);
    }

    //获取原始值-当前翻译字段
    public Object getOriginVal(){
        return params.findObjectVal(record);
    }

    //获取原始值
    public Object getOriginVal(String name){
        return ParamsUtils.getObjVal(record,name);
    }

    public void replace (Object fieldValue){
        if(translator.replace()){
            params.setValue(record,fieldValue);
        }
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        return params.getAnnotation(annotationClass);
    }
}
